import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-09
 * Time: 14:04
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        int count = 10;
        //一共十个任务
        CountDownLatch latch = new CountDownLatch(count);
        for (int i = 1; i <= count; i++) {
            int n = i;
            Thread thread = new Thread(()->{
                Random random = new Random();
                int time = (random.nextInt(5) + 1) * 1000;
                System.out.println("线程：" + n + "开始执行");
                System.out.println("线程：" + n + "执行完毕");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                latch.countDown();
            });
            thread.start();
        }
        //等待count减为0
        //await方法无参数就是死等
        //有参数可以传入时间
        latch.await();
        System.out.println("所有线程执行完毕");
    }
}